/*************
author:: libo
license:: MIT
*****************/

#include<Matrix/LB_Matrix.h>

#define Matrix4x4 LB_Matrix
void lb_matrix_free(LB_Matrix *m)
{
	if(m==NULL)
	{return;}	
	if(m->data!=0)
	{
		free(m->data);
	}
	free(m);
}
LB_MATRIX_FUNC(double)
LB_MATRIX_FUNC(float)
LB_Matrix *Projection(float FOV,float aspect,float zNear,float zFar)
{
    LB_Matrix *p=(LB_Matrix*)malloc(sizeof(LB_Matrix));
    lb_matrix_init_float(p,4,4);
	p->zero(p);
    float zRange=zNear-zFar;
    float tanHalfFOV=tan((FOV/2.0f));
	float* data=(float*)(p->data);
    data[0*4+0]=1.0/(tanHalfFOV*aspect);
    data[1*4+1]=1.0f/tanHalfFOV;
    data[2*4+2]=-(zNear+zFar)/zRange;

    // data[2*4+3]=1.0;
    // data[3*4+2]= 2.0f*zFar*zNear/zRange ; 

    data[2*4+3]=2.0f*zFar*zNear/zRange;
    data[3*4+2]=-1.0f;

	return p;
}

//相机内参转透视矩阵
//返回FOV,ASPECT, 近裁面的距离
double* camera2Proj(double*ssd,double f)
{
    double* re=(double*)malloc(sizeof(double)*3);
    re[0]=atan((ssd[1]/2.0)/f );
    re[1]=ssd[0]/ssd[1];
    re[2]=f;

    return re;
}


#undef Matrix4x4
